package edu.ar.modulo4.backingbean;

import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import edu.ar.modulo4.common.NavigationRule;
import edu.ar.modulo4.managedbeans.UsuarioManagedBean;

public class LoginBackingBean {

	private String usuario;
	private String password;
	private UsuarioManagedBean usuarioManagedBean;
	
	public LoginBackingBean() {
		this.usuario = "";
		this.password = "";
	}
	
	public String doLogin() {
		
		if ( this.usuario.equalsIgnoreCase( usuarioManagedBean.getUsuario() ) 
				&& this.password.equalsIgnoreCase( usuarioManagedBean.getPassword() ) ) {
			
			return NavigationRule.success.toString();
		}
		
		// add a message on the context
		addErrorMessage();
		
		return NavigationRule.fail.toString();
	}
	
	private void addErrorMessage() {
		ResourceBundle bundle = getResourceBundle();
		String msg = bundle.getString("lbl_errorLogin");
		
		FacesMessage facesMessage = new FacesMessage(msg);
		
		getContext().addMessage("errorLogin", facesMessage);	
	}
	
	protected FacesContext getContext() {
		return FacesContext.getCurrentInstance();
	}
	
	protected ResourceBundle getResourceBundle() {
		return ResourceBundle.getBundle("webmessages");
	}


	/********* GETTER & SETTER***********/
	
	public String getUsuario() {
		return usuario;
	}

	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsuarioManagedBean(UsuarioManagedBean usuarioManagedBean) {
		this.usuarioManagedBean = usuarioManagedBean;
	}

	public UsuarioManagedBean getUsuarioManagedBean() {
		return usuarioManagedBean;
	}	
}
